//
//  FMPersonViewController.m
//  PsychologyFM
//
//  Created by zhang on 16/1/22.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//


#import "FMDiantaiPlayViewController.h"
#import "FMCollectionModel.h"
#import "FMCollectTableViewCell.h"
#import "FMPersonHeaderView.h"
#import "FMPersonViewController.h"

@interface FMPersonViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation FMPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    [self getPersonInformation];
    [self getColletionInformation];
    [self addTableView];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(20, 40, 30, 30);
    [backButton setImage:[UIImage imageNamed:@"broadcasterBack"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backButton];
}
- (void)backButton:(UIButton *)backButton{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

-(void)addTableView{

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 90;
    
    self.headerView = [[FMPersonHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenHeight, kScreenHeight/5 + 130)];
//    self.headerView.model = nil;
    

    self.tableView.tableHeaderView = self.headerView;
    
    [self.tableView registerClass:[FMCollectTableViewCell class] forCellReuseIdentifier:@"PersonCell"];
    
    [self.view addSubview:_tableView];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    FMCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];
    FMCollectionModel *model = self.dataArray[indexPath.row];

    cell.model = model;
    
    return cell;

}
#pragma mark 点击收藏跳向对应的电台界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FMDiantaiPlayViewController *diantaiVC = [[FMDiantaiPlayViewController alloc]init];

//    取出收藏电台的信息
    FMCollectionModel *model = self.dataArray[indexPath.row];
    diantaiVC.value = model.id;
    
//    FMRootViewController *rootVC =   (FMRootViewController *) [UIApplication sharedApplication].windows[0].rootViewController;
  //  diantaiVC.rootVC = rootVC;
    [self.navigationController pushViewController:diantaiVC animated:YES];
    
   

}
- (void)getPersonInformation{

    NSString *str = @"http://bapi.xinli001.com/users/user_detail.json/?";
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    parDic[@"key"] = @"c0d28ec0954084b4426223366293d190";
//    parDic[@"rows"] = @"10";
    parDic[@"pk"] = self.user_id;
    

    parDic[@"offset"] = [NSString stringWithFormat:@"%ld",(long)self.offSet];
    

    
    //    get请求 进行字符串拼接
    //    1. 先把字典的键值对以一定格式存储在数组里面
    // 初始化一个字典用来后面保存格式化的字符串键值对
    NSMutableArray *strArray = [NSMutableArray array];
    // 遍历字典
    for (NSString *key in parDic) {
        // 以key=value形式存储键值对于数组中
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,parDic[key]];
        [strArray addObject:str];
    }
    //    2.将数组里面的元素 组成字符串
    NSString *arrayStr = [strArray componentsJoinedByString:@"&"];
    //    3.合成最后需要的网址
    NSString *urlStr = [str stringByAppendingString:arrayStr];

    
    [PKRequestManager requestWithUrlString:urlStr parDic:nil method:GET finish:^(NSData *data) {

        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dicData = dic[@"data"];
        FMPersonModel *model = [[FMPersonModel alloc]init];
        [model setValuesForKeysWithDictionary:dicData];
        self.headerView.model = model;

      
        dispatch_async(dispatch_get_main_queue(), ^{
            
           
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        ;
    }];
    
    

}
- (void)getColletionInformation{
//@" http://bapi.xinli001.com/fm2/favorite_list.json/?key=c0d28ec0954084b4426223366293d190&rows=10&offset=0&user_id=1000235569
    
    NSString *str = @"http://bapi.xinli001.com/fm2/favorite_list.json/?";
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"key"] = @"c0d28ec0954084b4426223366293d190";
    parDic[@"rows"] = @"10";
    
    parDic[@"offset"] = [NSString stringWithFormat:@"%ld",(long)self.offSet];
    parDic[@"user_id"] = self.user_id;
    //    get请求 进行字符串拼接
    //    1. 先把字典的键值对以一定格式存储在数组里面
    // 初始化一个字典用来后面保存格式化的字符串键值对
    NSMutableArray *strArray = [NSMutableArray array];
    // 遍历字典
    for (NSString *key in parDic) {
        // 以key=value形式存储键值对于数组中
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,parDic[key]];
        [strArray addObject:str];
    }
    //    2.将数组里面的元素 组成字符串
    NSString *arrayStr = [strArray componentsJoinedByString:@"&"];
    //    3.合成最后需要的网址
    NSString *urlStr = [str stringByAppendingString:arrayStr];
    
    
    [PKRequestManager requestWithUrlString:urlStr parDic:nil method:GET finish:^(NSData *data) {
        
        self.dataArray = [NSMutableArray array];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dicData = dic[@"data"];
        for (NSDictionary *dict in dicData) {
            FMCollectionModel *model = [[FMCollectionModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:model];
            
        }
      
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        ;
    }];
    
    


}

- (void)viewWillAppear:(BOOL)animated{

    

}

- (void)viewWillDisappear:(BOOL)animated{

    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
